/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.earnings.service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.jeeplus.modules.api.common.UUIDUtils;
import com.jeeplus.modules.api.utils.ClientCustomSSL;
import com.jeeplus.modules.api.utils.OtherUtils;
import com.jeeplus.modules.api.wxpay.EnterprisesPayment;
import com.jeeplus.modules.api.wxpay.WxPayUtils;
import com.jeeplus.modules.api.wxpay.WxTx;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.earnings.entity.Earnings;
import com.jeeplus.modules.earnings.mapper.EarningsMapper;

/**
 * 收益记录Service
 * @author zhych
 * @version 2021-08-18
 */
@Service
@Transactional(readOnly = true)
public class EarningsService extends CrudService<EarningsMapper, Earnings> {

	@Autowired
	private MemberService memberService;

	public Earnings get(String id) {
		return super.get(id);
	}
	
	public List<Earnings> findList(Earnings earnings) {
		return super.findList(earnings);
	}
	
	public Page<Earnings> findPage(Page<Earnings> page, Earnings earnings) {
		return super.findPage(page, earnings);
	}
	
	@Transactional(readOnly = false)
	public void save(Earnings earnings) {
		super.save(earnings);
	}
	
	@Transactional(readOnly = false)
	public void delete(Earnings earnings) {
		super.delete(earnings);
	}

	/**
	 * 微信提现
	 * @param money 提现金额
	 * @param remark 提现说明
	 * @return 提现结果
	 */
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void withdraw(Member member, String money, String remark, String appId, String appSecret, String mchNo, String certPath) throws Exception {
		WxTx wt = new WxTx();
		Integer amount1 = (new BigDecimal(money).multiply(new BigDecimal(100))).intValue();
		String signCode = wt.getSgin(member.getOpenid(), amount1, "提现入账", appId, mchNo, appSecret);//获取用户openid 和 用户要提现的金额  拿到签名
		EnterprisesPayment enterprisesPayment = wt.addEnterprisesPayment(member.getOpenid(), signCode, amount1, appId, mchNo);//拿到签名后加密得到要发送到对象数据
		String enterprisesPaymentXML = WxPayUtils.createDocumentForEnterprisesPayment(enterprisesPayment, amount1);  //封装xml 数据发送
		String returnXmlData = ClientCustomSSL.doRefund("https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers", enterprisesPaymentXML, mchNo, certPath);
		Map<String, String> resultMap = OtherUtils.readStringXmlOut(returnXmlData);
		if (resultMap.containsKey("result_code") && "SUCCESS".equals(resultMap.getOrDefault("result_code", ""))) {
			System.out.println("提现成功");
			System.out.println("返回结果：" + returnXmlData);
			//提现成功添加提现流水
			Earnings earnings = new Earnings();
			earnings.setIsNewRecord(true);
			earnings.setId(UUIDUtils.orderUuid(4));
			earnings.setEarningsType("2");
			earnings.setPayment(money);
			earnings.setRemark(remark);
			earnings.setU(member);
			super.save(earnings);
			// 该用户的余额扣减
			member.setBalance(new BigDecimal(member.getBalance()).subtract(new BigDecimal(money)).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
			memberService.save(member);
		}
	}
}